#include <stdio.h>
#include <unistd.h>
#include "msg_queue.h"
#include "status.h"
#include "struct.h"

static int msgid_ctos;
static int msgid_stoc;

static Msg_ctos msg_ctos;
static Msg_stoc msg_stoc;

void msg_send(void)
{
	if(ERROR == msgsnd(msgid_ctos,&msg_ctos,sizeof(msg_ctos)-4,0))
	{
		perror("msgsnd");
		_exit(-1);
	}
}

void msg_recv(void)
{
	if(ERROR == msgrcv(msgid_stoc,&msg_stoc,sizeof(msg_stoc),msg_stoc.m_type,0))
	{
		perror("msgrcv");
		_exit(-1);
	}

	show_msg(msg_stoc.m_str,1.5);
}

void show_menu_login(void)
{
	system("clear");
	puts("-----------------------");
	puts("[4]查询    [5]转帐");
	puts("[6]存款    [7]改码");
	puts("[8]取款    [0]退出");
	puts("-----------------------");
}

void show_menu(void)
{
	system("clear");
	puts("------------------------------------");
	puts("  [1]开户	        	[2]销户");
	puts("  [3]登录	        	[0]退出");
	puts("------------------------------------");
}

void open(void)
{
	printf("请输入用户名:");
	get_str(msg_ctos.m_acc.name,sizeof(msg_ctos.m_acc.name));

	printf("请输入密码:");
	get_pd(msg_ctos.m_acc.pswd,sizeof(msg_ctos.m_acc.pswd));

	printf("请输入预存金额:");
	scanf("%f",&msg_ctos.m_acc.balance);

	msg_ctos.m_type = M_OPEN;
	msg_send();
	msg_recv();
}

void save(void)
{
//	printf("请输入帐号:");
//	scanf("%d",&msg_ctos.m_acc.id);

//	printf("请输入密码:");
//	get_pd(msg_ctos.m_acc.pswd,sizeof(msg_ctos.m_acc.pswd));

	printf("请输入存款金额:");
	scanf("%f",&msg_ctos.m_acc.balance);
	
	msg_ctos.m_type = M_SAVE;
}

void take(void)
{
//	printf("请输入帐号:");
//	scanf("%d",&msg_ctos.m_acc.id);

//	printf("请输入密码:");
//	get_pd(msg_ctos.m_acc.pswd,sizeof(msg_ctos.m_acc.pswd));

	printf("请输入取款金额:");
	scanf("%f",&msg_ctos.m_acc.balance);

	msg_ctos.m_type = M_TAKE;

}

void query(void)
{
//	printf("请输入帐号:");
//	scanf("%d",&msg_ctos.m_acc.id);

//	printf("请输入密码:");
//	get_pd(msg_ctos.m_acc.pswd,sizeof(msg_ctos.m_acc.pswd));

	msg_ctos.m_type = M_QUERY;
}

void transf(void)
{
//	printf("请输入帐号:");
//	scanf("%d",&msg_ctos.m_acc.id);

//	printf("请输入密码:");
//	get_pd(msg_ctos.m_acc.pswd,sizeof(msg_ctos.m_acc.pswd));

	printf("请输入要转入的帐号:");
	get_str(msg_ctos.m_acc.name,sizeof(msg_ctos.m_acc.name));

	printf("请输入转账金额:");
	scanf("%f",&msg_ctos.m_acc.balance);

	msg_ctos.m_type = M_TRANSF;
}

void destroy(void)
{
	printf("请输入要注销的帐号:");
	scanf("%d",&msg_ctos.m_acc.id);

	printf("请输入要注销的用户名:");
	get_str(msg_ctos.m_acc.name,sizeof(msg_ctos.m_acc.name));

	printf("请输入密码:");
	get_pd(msg_ctos.m_acc.pswd,sizeof(msg_ctos.m_acc.pswd));

	msg_ctos.m_type = M_DESTROY;
	msg_send();
	msg_recv();
}

void modify(void)
{
//	printf("请输入帐号：");
//	scanf("%d",&msg_ctos.m_acc.id);
	
	printf("请输入原密码：");
	get_pd(msg_ctos.m_acc.pswd,sizeof(msg_ctos.m_acc.pswd));
	
	printf("请输入修改的密码：");
	scanf("%s",msg_ctos.m_acc.name);
	
	msg_ctos.m_type = M_MODIFY;
	
}

void login(void)
{
	printf("请输入账号：");
	scanf("%d",&msg_ctos.m_acc.id);

	printf("请输入密码:");
	get_pd(msg_ctos.m_acc.pswd,sizeof(msg_ctos.m_acc.pswd));

	msg_ctos.m_type = M_LOGIN;
	
	msg_send();
	msg_recv();
	if(0 == strcmp(msg_stoc.m_str,"登录成功"))
	{
		while(1)
		{
			sleep(2);
			show_menu_login();
			switch(get_cmd('0','9')-'0')
			{
				case M_EXIT: return;
				case M_SAVE: save();break;
				case M_TAKE: take();break;
				case M_QUERY: query();break;
				case M_TRANSF: transf();break;
				case M_MODIFY: modify();break;
			}
			msg_send();
			msg_recv();
			//if(0 == strcmp(msg_stoc.m_str,"密码修改成功，请重新登录"));
			//return;
		}
	}
	
}

int main()
{
	msgid_ctos = get_ctos_msg();
	msgid_stoc = get_stoc_msg();

	msg_stoc.m_type = msg_ctos.m_pid = getpid(); 

	while(true)
	{
		sleep(2);
		show_menu();
		switch(get_cmd('0','7') - '0')
		{
			case M_EXIT: return;
			case M_OPEN: open(); break;
	//		case M_SAVE: save(); break;
	//		case M_TAKE: take(); break;
	//		case M_QUERY: query(); break;
	//		case M_TRANSF: transf(); break;
			case M_DESTROY:	destroy(); break;
			case M_LOGIN: login();break;
	//		case M_MODIFY: modify();break;
		}

		
	}
}
